package com.gty.controller;

import com.alibaba.fastjson.JSON;

import com.gty.entity.UserHero;
import com.gty.lolutil.BaseUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class UserController {
    /**
     * 获取当前用户 可用的所有英雄
     */
    public List<UserHero> getUserAllHero(String baseUrl, CloseableHttpClient httpClient) {
        System.err.println(baseUrl);
        HttpGet httpGet = new HttpGet(baseUrl + "/lol-champions/v1/owned-champions-minimal");
        List<UserHero> heroList = new ArrayList();
        CloseableHttpResponse execute = null;
        try {

            execute = httpClient.execute(httpGet);
            if (execute.getStatusLine().getStatusCode() == 200) {
                //获取json数据
                String json = BaseUtil.getJson(execute.getEntity().getContent());
                List<Map<String, Object>> listMap;
                //封装到实体类中
                listMap = JSON.parseObject(json, List.class);
                // System.err.println(listMap);
                for (int i = 0; i < listMap.size(); i++) {
                    Map<String, Object> map = listMap.get(i);
                    //System.err.println(listMap.get(i));
                    heroList.add(new UserHero((Integer) map.get("id"), (String) map.get("name"), (String) map.get("title")));
                }
                return heroList;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                execute.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return null;
    }

    /**
     * 获取选英雄时 召唤师id
     * -1表示未获取到
     */
    public long getSummonerId(String baseUrl, CloseableHttpClient httpClient) {
        HttpGet httpGet = new HttpGet(baseUrl + "/lol-login/v1/session");
        CloseableHttpResponse execute = null;
        try {
            execute = httpClient.execute(httpGet);
            if (execute.getStatusLine().getStatusCode() == 200) {
                String json = BaseUtil.getJson(execute.getEntity().getContent());
                Map<String, Object> map = JSON.parseObject(json, Map.class);
                return Long.parseLong(map.get("summonerId") + "");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                execute.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    /**
     * 发送秒选请求
     */
    public int instantSelectHero(String baseUrl, CloseableHttpClient httpClient, String heroId) {
        //首先发送/lol-champ-select/v1/session 获取对局会话响应
        long summonerId = getSummonerId(baseUrl, httpClient);
        //System.err.println("summonerId=="+summonerId);
        if(summonerId!=-1){
            int cellId=-1;
            CloseableHttpResponse execute=null,execute2=null;
            try {
                execute=httpClient.execute(new HttpGet(baseUrl+"/lol-champ-select/v1/session"));
                //System.out.println(execute.getStatusLine().getStatusCode());
                if (execute.getStatusLine().getStatusCode()==200){
                    String json = BaseUtil.getJson(execute.getEntity().getContent());
                    Map<String,Object> map=JSON.parseObject(json,Map.class);

                    List<Map<String,Object>> listMap=JSON.parseObject(JSON.toJSONString(map.get("myTeam")),List.class);
                    //遍历 myTeam的数组
                    for(Map<String,Object> m:listMap){
                        //System.err.println(m);
                        if ((Long)m.get("summonerId")==summonerId){
                            //找到队伍中相匹配的id 抽取 "cellId"
                            cellId=(Integer) m.get("cellId");
                        }
                    }
                    //cellId 拿到 "actions"数组里 和"actorCellId": 0,比对
                    //System.err.println("cellId=="+cellId);
                    //如果相同 就获取当前json对象 进行修改 发送锁定请求
                    List<List<Map<String,Object>>> listMap2=JSON.parseObject(JSON.toJSONString(map.get("actions")),List.class);
                    for(Map<String,Object> m:listMap2.get(0)){
                        //System.err.println(m);
                        if ((Integer)m.get("actorCellId")==cellId){
                            //开始修改这个m 集合的数据
                            m.put("championId",heroId);
                            m.put("completed",true);
                            String body = JSON.toJSONString(m);
                            int id =(Integer) m.get("id");
                            //发送秒选请求
                            HttpPatch httpPatch = new HttpPatch(baseUrl+"/lol-champ-select/v1/session/actions/"+id);
                            //httpPatch.setHeader("Accept", "application/json");
                            httpPatch.setHeader("Content-Type","application/json");
                            httpPatch.setEntity(new StringEntity(body));

                             execute2 = httpClient.execute(httpPatch);
                            //System.out.println("传送的json"+body);
                            if(execute2.getStatusLine().getStatusCode()==200){
                                System.out.println("秒选成功");
                                break;
                            }

                        }
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    execute.close();
                    if(execute2!=null)
                        execute2.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
        return 0;
    }
}
